﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace PixelWarfare
{
    /// <summary>
    /// Basic menu item that only consists of a lable and event when its clicked.
    /// </summary>
    class SelectionMenuItem : IMenuItem
    {
        #region Fields

        private string text;

        private int x;
        private int y;

        private Font font;

        private Color onColor;          // Color when the mouse is over the item.
        private Color offColor;         // Color when the mouse is noy over the item.

        private bool mouseOver;         // If the mouse is over the menu item.
        private RectangleF rec;         // The rectangle the menu item occupies.
        private OnClick clickEvent;     // The method that runs when the button is clicked.

        #endregion

        #region Constructors

        /// <summary>
        /// Create a new SelectionMenuItem with the given parameters.
        /// </summary>
        /// <param name="text">"Main item text"</param>
        /// <param name="x">"X coordinate to center at."</param>
        /// <param name="y">"X coordinate to center at."</param>
        /// <param name="font">"Font to draw the text with."</param>
        /// <param name="onColor">"Color when the mouse is over the item."</param>
        /// <param name="offColor">"Color when the mouse is not over the item."</param>
        /// <param name="clicked">"Method to run when the item is clicked."</param>
        public SelectionMenuItem(string text, int x, int y, Font font, Color onColor, Color offColor, OnClick clicked)
        {
            this.text = text;

            this.x = x;
            this.y = y;
            this.font = font;

            this.onColor = onColor;
            this.offColor = offColor;

            rec = new RectangleF(); // Bounds are defined later since we need a graphics object.

            mouseOver = false;

            clickEvent = clicked;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Draw the text centered at the given coordinates.
        /// </summary>
        /// <param name="g">The graphics to drawn on.</param>
        public void Draw(Graphics g)
        {
            if (rec.IsEmpty) // Define the bounds the first time we draw.
            {
                SizeF bounds = g.MeasureString(text, font); // So we know where to centre.
                rec.X = (x - bounds.Width / 2);
                rec.Y = (y - bounds.Height / 2);
                rec.Height = bounds.Height;
                rec.Width =  bounds.Width;
            }

            if (mouseOver)
            {
                g.DrawString(text, font, new SolidBrush(onColor), rec);
            }
            else
            {
                g.DrawString(text, font, new SolidBrush(offColor), rec);
            }

        }

        /// <summary>
        /// If the mouse is over the item then change mouse over to true
        /// which causes the text to be drawn in the onColor.
        /// Otherwise set it to be false and the text is drawn in the offColor.
        /// </summary>
        /// <param name="x">X coordinate of the mouse.</param>
        /// <param name="y">X coordinate of the mouse.</param>
        public void MouseMove(int x, int y)
        {
            if (rec.Contains(x, y))
            {
                mouseOver = true;
            }
            else
            {
                mouseOver = false;
            }
        }

        /// <summary>
        /// If the mouse is pressed check if its on the text,
        /// if so run the clickEvent.
        /// </summary>
        /// <param name="x">X coordinate of the mouse click.</param>
        /// <param name="y">X coordinate of the mouse click.</param>
        public void MousePress(int x, int y)
        {
            if (rec.Contains(x, y))
            {
                clickEvent();
            }
        }

        /// <summary>
        /// Nothing needs to happen on update.
        /// </summary>
        public void Update() { }

        #endregion
    }
}
